package pl.pelcra.cesar.spider.webcrawler.sites;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import pl.pelcra.cesar.data.interfaces.ICommonData;
import pl.pelcra.cesar.spider.data.MapLangUrl;
import pl.pelcra.cesar.spider.webcrawler.CSWebURL;
import pl.pelcra.cesar.spider.webcrawler.interfaces.BasePageWebTasks;
import pl.pelcra.cesar.spider.webcrawler.interfaces.FilterRules;
import pl.pelcra.cesar.spider.webcrawler.interfaces.HttpRequestMethod;
import pl.pelcra.cesar.spider.webcrawler.interfaces.IPageActions;
import pl.pelcra.cesar.spider.webcrawler.interfaces.WebTask;
import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;

public class EuroparlPageWebTasks extends BasePageWebTasks {

	private final Logger logger = Logger.getLogger(EuroparlPageWebTasks.class);

	public EuroparlPageWebTasks() {

	}

	public EuroparlPageWebTasks(String parameters) {
		super(parameters);
	}

	@Override
	public void init(WebURL startWebUrl, ICommonData<MapLangUrl> threadData) {
		this.setSpiderThreadData(threadData);

		WebTask articlesLanguageWikiWebTask = new WebTask();
		articlesLanguageWikiWebTask.addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask webTask, Page page,
					HtmlParseData htmlParseData) {

				MapLangUrl map = new MapLangUrl();

				for (WebURL url : webTask.getListWebUrl()) {
					map.put(url.getURL().toLowerCase()
							.substring(url.getURL().length() - 2),
							new CSWebURL(url));
				}

				if (map.size() == 0) {
					EuroparlPageWebTasks.this.logger.info(String.format(
							"NoTranslationPage = %s", webTask.getCurWebURL()
									.getURL()));
				}

				String urlValue = webTask.getCurWebURL().getURL().toLowerCase()
						.replace("&language=en", "");
				map.put(urlValue.substring(urlValue.length() - 2),
						new CSWebURL(webTask.getCurWebURL()));

				EuroparlPageWebTasks.this.getSpiderThreadData().put(map);
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {

				String currentUrl = webTask.getCurWebURL().getURL();
				// currentUrl = Tools.normalizeURLRFC3986(currentUrl);
				currentUrl = currentUrl.replace("language=EN&", "");
				currentUrl = currentUrl.replace("&language=EN", "");
				currentUrl = currentUrl.replace("?", "\\?");

				String matchRegexUrl = currentUrl.substring(0,
						currentUrl.length() - 2);
				matchRegexUrl += "\\w{2,2}";

				if (webURL.getURL().matches(matchRegexUrl)) {
					return true;
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
				// for (WebURL url : wikiWebTask.getListWebUrl()) {
				// Tools.writeFile(url.getURL() + Tools.NEWLINE,
				// "D:\\links.txt", true);
				// }
				// Tools.writeFile(Tools.NEWLINE, "D:\\links.txt", true);
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
			}
		});

		WebTask pagesWikiWebTask = new WebTask(articlesLanguageWikiWebTask);
		pagesWikiWebTask.addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {

				String matchRegexUrl = "http://www.europarl.europa.eu/sides/getDoc.do\\?language=.+TEXT.+IM.+PRESS.+";

				if (webURL.getURL().toLowerCase()
						.matches(matchRegexUrl.toLowerCase())) {
					return true;
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {
				return null;
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.GET;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				return null;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return false;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
			}
		});

		setMainWebTask(new WebTask(pagesWikiWebTask));
		getMainWebTask().setMapParametrs(this.getMapParams());

		getMainWebTask().setCurWebURL(startWebUrl);

		getMainWebTask().addIPageActions(new IPageActions() {

			@Override
			public void afterProcessPage(WebTask wikiWebTask, Page page,
					HtmlParseData htmlParseData) {
				for (WebURL url : wikiWebTask.getListWebUrl()) {
					EuroparlPageWebTasks.this.logger.info(url.getURL());
				}
			}

			@Override
			public void beforeProcessPage(HtmlParseData htmlParseData) {
			}

			@Override
			public boolean filterUrl(WebURL webURL, WebTask webTask) {

				String matchRegexUrl = "http://www.europarl.europa.eu/parlArchives/pressArch/search/generalSearch.do\\?language=EN&startValue=\\d{1,4}";

				if (webURL.getURL().toLowerCase()
						.matches(matchRegexUrl.toLowerCase())) {
					return true;
				}
				return false;
			}

			@Override
			public void finishProcessWebTasks(WebTask wikiWebTask) {
				for (WebURL url : wikiWebTask.getListWebUrl()) {
					EuroparlPageWebTasks.this.logger.info(url.getURL());
				}
			}

			@Override
			public WebURL getFilterParallelWebUrl(FilterRules wikiFilterRules,
					List<WebURL> listWebUrl) {

				String integerValue;
				int tmpIntegerValue;
				HashMap<Integer, WebURL> hashMap = new HashMap<Integer, WebURL>();

				for (WebURL url : listWebUrl) {
					integerValue = url.getURL();
					integerValue = integerValue.substring(integerValue.indexOf(
							"=", integerValue.length() - 5) + 1);
					tmpIntegerValue = Integer.parseInt(integerValue);

					hashMap.put(tmpIntegerValue, url);
				}

				ArrayList<Integer> listInt = new ArrayList<Integer>(hashMap
						.keySet());
				Integer[] tab = new Integer[listInt.size()];
				tab = listInt.toArray(tab);
				Arrays.sort(tab);

				if (tab.length == 1) {
					return null;
					// return hashMap.get(tab[0]);
				} else if (tab[tab.length - 2] + 30 == tab[tab.length - 1]) {
					return hashMap.get(tab[tab.length - 1]);
				} else {
					return hashMap.get(tab[tab.length - 2]);
				}
			}

			@Override
			public HttpRequestMethod getHttpRequestMethod() {
				return HttpRequestMethod.POST;
			}

			@Override
			public List<BasicNameValuePair> getParameterValuePairs(
					HashMap<String, String> mapParameters) {
				ArrayList<BasicNameValuePair> listPostParams = new ArrayList<BasicNameValuePair>();

				// listPostParams.add(new BasicNameValuePair("legIdBean.legId",
				// "7"));
				// listPostParams.add(new BasicNameValuePair(
				// "productTypeBean.productTypeString", "STO"));

				listPostParams.add(new BasicNameValuePair(
						"categoryBean.categoryCode", ""));
				listPostParams.add(new BasicNameValuePair(
						"topicBean.idBusiness", "7"));
				listPostParams.add(new BasicNameValuePair(
						"indexSearchTypeBean.miType", "TITLE"));
				listPostParams.add(new BasicNameValuePair(
						"indexSearchValueBean.miValue", ""));
				listPostParams.add(new BasicNameValuePair("language", "EN"));

				if (mapParameters != null) {
					for (Map.Entry<String, String> entry : mapParameters
							.entrySet()) {
						listPostParams.add(new BasicNameValuePair(entry
								.getKey(), entry.getValue()));
					}
				}
				return listPostParams;
			}

			@Override
			public boolean isCookieRequired() {
				return false;
			}

			@Override
			public boolean isNextParallelPage(WebTask wikiWebTask,
					WebURL currentParallelWebUrl, WebURL filterParallelWebUrl) {
				if (currentParallelWebUrl != null
						&& filterParallelWebUrl != null
						&& currentParallelWebUrl != filterParallelWebUrl) {
					return true;
				}
				return false;
			}

			@Override
			public boolean isParallelProcess() {
				return true;
			}

			@Override
			public void processListURLs(WebTask wikiWebTask) {
				if (wikiWebTask.getListWebUrl().size() <= 1) {
					WebURL webURL = new WebURL();
					webURL.setURL("http://www.europarl.europa.eu/parlArchives/pressArch/search/generalSearch.do?language=EN&startValue=0");
					wikiWebTask.addWebURL(webURL);
				}
			}
		});
	}
}